package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.apache.ibatis.javassist.expr.Expr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private DeptServiceImpl deptService;

    @Autowired
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> queryEmpPage(EmpQueryParam param) {
        //分页条件
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        //排序条件
        page.addOrder(OrderItem.desc("update_time"));
        //拾取查询条件
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
        //用户主动输入条件
        Page<Emp> p = lambdaQuery().like(name != null && !name.isEmpty(), Emp::getName, name)
                .eq(gender != null, Emp::getGender, gender)
                .ge(begin != null, Emp::getEntryDate, begin)
                .le(end != null, Emp::getEntryDate, end)
                .page(page);

        List<Emp> emps = p.getRecords();
        if (!emps.isEmpty()) {
            List<Dept> depts = deptService.list();
            Map<Long, String> empToDept = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
            emps.forEach(emp -> {
                if (emp.getDeptId() != null) {
                    emp.setDeptName(empToDept.get(emp.getDeptId()));
                }
            });
        }
        PageResult<Emp> pageResult = new PageResult<Emp>(p.getTotal(), emps);
        return pageResult;
    }

    @Override
    @Transactional
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        save(emp);

        Long empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList == null || exprList.isEmpty()) {
            return;
        }
        exprList.forEach(expr -> expr.setEmpId(empId));
        empExprService.saveBatch(exprList);

    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        emp.setDeptName(deptService.getById(emp.getDeptId()).getName());
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));
        return emp;
    }
    @Override
    @Transactional
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));

        List<EmpExpr> exprList = emp.getExprList();
        if (exprList == null || exprList.isEmpty()) {
            return;
        }
        exprList.forEach(expr -> expr.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

    @Override
    @Transactional
    public void removeEmpByIds(List<Integer> ids) {
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }
}
